<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 导入</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="/static/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="orderInfo">详细信息</a>
    <a class="layui-btn layui-btn-xs" lay-event="more">更多 <i class="layui-icon layui-icon-down"></i></a>
</script>
<script>
    layui.use(['dropdown', 'util', 'layer', 'table'], function () {
        var dropdown = layui.dropdown;
        var table = layui.table;
        var $ = layui.jquery;
        table.render({
            elem: '#currentTableId',
            url: '{:url("data/gettorder")}',
            toolbar: '#toolbarDemo',
            // defaultToolbar: ['filter', 'exports', 'print', {title: '提示',layEvent: 'LAYTABLE_TIPS',icon: 'layui-icon-tips'}],
            cols: [[
                {field: 'tk_create_time', width: 150, title: '创建时间'},
                {field: 'order_type', width: 100, title: '订单平台'},
                {field: 'robot_id', width: 100, title: '机器人编号',hide:true},
                {field: 'openid', title: '用户ID'},
                {field: 'item_title', width: 120, title: '商品标题'},
                {field: 'trade_id', width: 200, title: '子订单编号'},
                {field: 'trade_parent_id', width: 200, title: '订单编号'},
                {field:'refund_tag', title: '主图',hide:true},
                {field: 'item_id', title: '商品ID'},
                {field: 'pub_share_pre_fee', title: '联盟佣金'},
                {field: 'total_commission_rate', title: '佣金比例'},
                {field: 'refund_tag', title: '绑定时间',hide:true},
                {field: 'refund_tag', title: '解冻时间',hide:true},
                {
                    field: 'tk_status', title: '订单状态', align: 'center', width: 100, templet: function (d) {
                        if (d.tk_status == 12) {
                            return '<span class="layui-badge layui-bg-cyan">订单付款</span>';
                        } else if (d.tk_status == 13) {
                            return '<span class="layui-badge layui-bg-red">订单失效</span>';

                        } else if (d.tk_status == 14) {
                            return '<span class="layui-badge layui-bg-blue">订单成功</span>';

                        } else if (d.tk_status == 3) {
                            return '<span class="layui-badge layui-bg-green">订单结算</span>';

                        }
                    }
                },

                {field: 'fl_status', title: '补贴状态', align: 'center', width: 100},

                {title: '操作', toolbar: '#barDemo', width: 200},
                //   {field:'refund_tag', title: '商品ID'},
                //   {field:'refund_tag', title: '付款金额'}
            ]],

            limit: 20,
            page: true,

        });

        //行工具事件
        table.on('tool(currentTableFilter)', function (obj) {
            var that = this;
            var data = obj.data;

            if (obj.event === 'orderInfo') {
                var index = layer.open({
                    title: '详细信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: "{:url('api/ord/edit')}?id=" + data.id
                });

            } else if (obj.event === 'more') {
                //更多下拉菜单
                dropdown.render({
                    elem: that
                    , show: true //外部事件触发立即显示
                    , data: [
                        {title: '解除绑定'},
                        {title: '绑定微信'},
                        {title: '解除冻结'},
                        {title: '结算订单'},
                        {title: '订单退款'},
                        {title: '转绑用户'},
                        {title: '店铺拉黑'},
                        {title: '删除', id: "del"}
                    ]
                    , click: function (datas, othis) {
                        // console.log('找技术合作');

                        if (datas.title === '解除绑定') {
                            if (data.fl_status == '已退款') {
                                layer.alert('<span class="layui-badge layui-bg-red">返利状态已退款</span> 的订单不支持解绑', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            if (data.status == 13) {
                                layer.alert('<span class="layui-badge layui-bg-red">订单状态失效</span> 的订单不支持解绑', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            //传父订单ID(大订单号,机器人编号，数据库id)
                            orderThawPay(data.trade_parent_id, data.robot_id, data.id);


                        } else if (datas.title === '绑定微信') {

                            if (data.fl_status != '未绑定') {
                                layer.alert("请选择未绑定的订单", {icon: 2, title: '温馨提示'});
                                return;
                            }
                            if (data.tk_status == 13) {
                                layer.alert('<span class="layui-badge layui-bg-red">订单状态失效</span> 的订单不支持绑定', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            var index = layer.open({
                                title: '请输入要绑定的微信ID',
                                area: ['450px', '170px'],
                                btn: ['确认', '取消'],
                                content: '<input type="text" id="openid"  class="layui-input" value="" >',
                                yes: function (index, layero) {
                                    //按钮【确认修改】的回调
                                    var openid = layero.find('#openid').val();
                                    //传父订单ID(订单号,机器人编号，用户ID,数据库id)
                                    orderBangding(data.trade_parent_id, data.robot_id, openid, data.id);
                                }
                            })


                        } else if (datas.title === '解除冻结') {
                            if (data.fl_status != '冻结中') {
                                layer.alert('请选择 <span class="layui-badge layui-bg-red">返利状态冻结中</span> 的订单', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            //传子订单ID(订单号,机器人编号,数据库id)
                            orderThawFor(data.trade_id, data.robot_id, data.id);

                        } else if (datas.title === '结算订单') {
                            if (data.status == 13) {
                                layer.alert('<span class="layui-badge layui-bg-red">订单状态失效</span> 的订单不支持结算', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            if (data.fl_status != '待收货') {
                                layer.alert('请选择 <span class="layui-badge layui-bg-red">返利状态待收货</span> 的订单', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            //传父订单ID
                            orderPub(data.trade_parent_id, data.robot_id, data.id);

                        } else if (datas.title === '订单退款') {

                            if (data.status == 13) {
                                layer.alert('<span class="layui-badge layui-bg-red">订单状态失效</span> 的订单不支持退款', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            if (data.fl_status == '已退款') {
                                layer.alert('<span class="layui-badge layui-bg-red">返利状态已退款</span> 的订单不支持退款', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            //传子订单ID
                            orderInv(data.trade_id, data.robot_id, data.id);


                        } else if (datas.title === '转绑用户') {
                            if (data.tk_status == 13) {
                                layer.alert('<span class="layui-badge layui-bg-red">订单状态失效</span> 的订单不支持转绑', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            if (data.fl_status == '未绑定') {
                                layer.alert('<span class="layui-badge layui-bg-red">返利状态未绑定</span> 的订单不支持转绑', {
                                    icon: 2,
                                    title: '温馨提示'
                                });
                                return;
                            }
                            //传子父订单ID
                            orderTransfer(data.trade_parent_id, data.robot_id, data.id);


                        }
                        if (datas.id === 'del') {
                            layer.confirm('真的删除行么', function (index) {
                                obj.del();
                                layer.close(index);
                            });
                        }

                    }


                });
            }
        });


        //解除冻结订单

        function orderThawFor(order_id, robot_id, id) {
            var data = {'order_id': order_id, 'type': 1, 'robot_id': robot_id, 'id': id}
            ajaxs('orderThawFor', data);
        }

        //解除订单绑定
        function orderThawPay(order_id, robot_id, id) {
            var data = {'order_id': order_id, 'type': 1, 'robot_id': robot_id, 'id': id}
            ajaxs('orderThawPay', data);
        }

        //绑定订单待微信
        function orderBangding(order_id, robot_id, wx_id, id) {
            var data = {'order_id': order_id, 'type': 1, 'robot_id': robot_id, 'openid': wx_id, 'id': id}
            ajaxs('orderBangding', data);
        }

        //结算订单状态
        function orderPub(order_id, robot_id, id) {
            var data = {'order_id': order_id, 'type': 1, 'robot_id': robot_id, 'id': id}
            ajaxs('orderPub', data);
        }

        //退款订单状态
        function orderInv(order_id, robot_id, id) {
            var data = {'order_id': order_id, 'type': 1, 'robot_id': robot_id, 'id': id}
            ajaxs('orderInv', data);
        }

        //订单转绑用户
        function orderTransfer(order_id, robot_id, id) {
            layer.open({
                title: '请输入要转移的微信ID',
                area: ['450px', '170px'],
                btn: ['确认', '取消'],
                content: '<input type="text" id="wxId"  class="layui-input" value="" >',
                yes: function (index, layero) {
                    //按钮【确认修改】的回调
                    var wx_id = layero.find('#wxId').val();
                    //传父订单ID
                    if (wx_id.length < 5) {
                        layer.alert('请输入正确的微信ID', {icon: 2, title: '温馨提示'});
                        return;
                    }
                    var data = {'order_id': order_id, 'type': 1, 'openid': wx_id, 'robot_id': robot_id, 'id': id}
                    ajaxs('orderTransfer', data);
                }
            })
        }

        //公共函数
        function ajaxs(typeName, data) {
            $.ajax({
                url: "{:url('manage')}",
                type: 'GET',
                dataType: 'json',
                data: {'typeName': typeName, data: JSON.stringify(data)},

                beforeSend: function () {
                    //请求前加载遮罩
                    loadIndex = layer.load(1, {
                        shade: [0.1, '#fff']
                    });
                },
                complete: function () {
                    //请求结束后关闭遮罩
                    layer.close(loadIndex);
                },
                success: function (d) {
                    if (d.status == 1) {
                        // taskStatus(d.identifier);
                        layer.msg(d.msg);
                    } else {
                        layer.msg(d.msg);

                    }
                },
                error: function (d) {
                    console.log('获取配置失败')
                }
            })
        }

        //店铺黑名单(商品ID)
        function shopBloc(goods_id) {
            $('#goodsId').val(goods_id);
            layui.layer.open({
                title: "添加店铺",
                type: 1,
                area: ['380px', '420px'],
                btn: ['确认修改', '取消操作'],
                content: $('#shopBloc'),
                yes: function (index, layero) {
                    var goodsId = goods_id, gruopName = layero.find('#gruopName').val(),
                        blockDesc = layero.find('#blockDesc').val();
                    if (goodsId.length < 5) {
                        layer.alert('请输入正确的淘宝商品ID');
                        return;
                    }
                    if (blockDesc.length < 3) {
                        layer.alert('备注不能小于3个汉字');
                        return;
                    }
                    $.ajax({
                        url: '../fangLu/data.php',
                        type: 'GET',
                        dataType: 'json',
                        data: {
                            typeName: 'addShop',
                            goodsId: goodsId,
                            gruopName: gruopName,
                            blockDesc: blockDesc,
                            type: 1
                        },
                        beforeSend: function () {
                            loadIndex = layer.load(1, {
                                shade: [0.1, '#fff']
                            });
                        },
                        complete: function () {
                            layer.close(loadIndex);
                        },
                        success: function (d) {
                            if (d.status == 1) {
                                //操作成功关闭窗口
                                layer.msg(d.msg, {time: 3000})
                                layui.layer.close(index)
                            } else {
                                layer.alert(d.msg, {icon: 2, title: '温馨提示'});
                            }
                        },
                        error: function (d) {
                            layer.alert('操作失败');
                        }
                    })
                }
            })
        }

        //任务状态（队列的标志符）
        function taskStatus(identifier) {
            var setId = setInterval(function () {
                var typeName = 'taskStatus';
                $.ajax({
                    url: "{:url('manage')}",
                    type: 'GET',
                    dataType: 'json',
                    data: {'typeName': typeName, 'identifier': identifier},
                    success: function (d) {
                        if (d.status == 1) {
                            layer.msg(d.msg);
                            clearInterval(setId);//停止
                        }
                    },
                    error: function (d) {
                        console.log('获取配置失败')
                    }
                })
            }, 3000);
        }


    });
</script>
</body>


</html>